package com.joonhat.MonkeyFlash.hotkey;

import java.util.Vector;

import com.joonhat.MonkeyFlash.hotkey.KeyRecover;

import net.rim.blackberry.api.homescreen.HomeScreen;
import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.ApplicationManager;
import anywhere.StringEditor;

public class Hotkey {

	public static void setHotkeyInStatup(char newHotkey, 
			ApplicationDescriptor currentApp, boolean afterExit) {
		if (inStartup() == false) return;
		setHotkey(newHotkey, currentApp);
		if (afterExit)
			System.exit(0);
	}
	
	public static void replaceCurrentAppHotkey(char newHotkey){
		replace(newHotkey, ApplicationDescriptor.currentApplicationDescriptor());
	}
	
	// key가 0으로 입력될때를 조심
	protected static void replace(char newHotkey, ApplicationDescriptor targetApp) {
		clearHotkey(newHotkey);
		setHotkey(newHotkey, targetApp);
	}

	private static void clearHotkey(char clearKey){
		if (isVoidHotkey(clearKey)) 
			return;
		Vector vec = HotkeyManager.getDescriptorsFor(clearKey);
		for (int i = 0; i < vec.size(); i++){
			setIconName(HotkeyCalculator.getDelAtUnderbar(getName(vec, i)),
					getDescriptor(vec, i));
		}
	}
	
	private static void setHotkey(char newHotkey, ApplicationDescriptor targetApp) {
		setNewHotkey(newHotkey, targetApp);
		setDiscardedHotkey(newHotkey);
	}

	private static void setDiscardedHotkey(char newHotkey) {
		KeyRecover.recoverDiscardedHotkey(newHotkey);
	}

	private static void setNewHotkey(char newHotkey, ApplicationDescriptor targetApp) {
		if (isVoidHotkey(newHotkey)) { 
			//단축키로 0이 입력되면 언더바를 지움
			setIconName(HotkeyCalculator.getDelAtUnderbar(getDefaultIconName(targetApp)), targetApp);
		} else {
			setIconName(HotkeyCalculator.getAddUnderbarSet(newHotkey, targetApp), targetApp);
		}
	}

	private static String getDefaultIconName(ApplicationDescriptor checkApp) {
		if (HotkeyManager.isRimApp(checkApp)){
			return checkApp.getDescription().toString();
		} else {
			return checkApp.getName();
		}
	}

	private static String getName(Vector vec, int i) {
		return getDescriptor(vec, i).getName();
	}

	private static ApplicationDescriptor getDescriptor(Vector vec, int i) {
		return (ApplicationDescriptor) vec.elementAt(i);
	}
	
	private static boolean inStartup() {
		return ApplicationManager.getApplicationManager().inStartup();
	}
	
	static boolean isVoidHotkey(char newHotkey) {
		return newHotkey == 0;
	}
	

	static void setIconName(String name, ApplicationDescriptor descriptor){
		HomeScreen.setName(name, descriptor);
	}
	
}

class HotkeyCalculator{
	
	private static final char PARENTHESES_OPEN = '[';
	private static final char PARENTHESES_CLOSE = ']';

	public static char getAtCharUnderBar(String src) throws NotUnderbarExcetion,
			InitUnderbarException {
		int underbarIndex = getUnderbarIndex(src);
		if (underbarIndex == -1) throw new NotUnderbarExcetion();
		if (underbarIndex == 0) throw new InitUnderbarException();
		return src.charAt(underbarIndex - 1);
	}
	
	public static String getAddUnderbarSet(char targetKey, ApplicationDescriptor app) {
		return new StringBuffer().append(getDelAtUnderbar(app.getName()))
				.append(" " + PARENTHESES_OPEN).append(targetKey).append('\u0332')
				.append(PARENTHESES_CLOSE).toString();
	}
	
	public static String getDelUnderbarSet(final String src) {
		final String primaryDelString = StringEditor.delete(src, PARENTHESES_CLOSE);
		final String secondaryDelString = getDelAtUnderbar(primaryDelString);
		return StringEditor.delete(secondaryDelString, PARENTHESES_OPEN);
	}
	
	public static String getDelAtUnderbar(String src) {
		return StringEditor.delete(src, '\u0332');
	}
	
	private static int getUnderbarIndex(String src) {
		return src.lastIndexOf('\u0332');
	}
}

class NotUnderbarExcetion extends Exception{ public NotUnderbarExcetion() {} }
class InitUnderbarException extends Exception{ public InitUnderbarException() {} }


